package me.sdau.spider.kafkaprocess.comm.config;

import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * @author zj
 * @date 2018/4/3 16:14
 */
@Configuration
public class ElasticSearchConfig {

    @Value("${spring.data.elasticsearch.cluster-nodes}")
    private String clusterNodes;
    @Value("${spring.data.elasticsearch.cluster-name}")
    private String clusterName = "elasticsearch";
    @Value("${spring.data.elasticsearch.properties.port}")
    private Integer port = 9300;

    @Bean
    public TransportClient transportClient() throws UnknownHostException {
        InetSocketTransportAddress node = new InetSocketTransportAddress(
                InetAddress.getByName(clusterNodes)
//                InetAddress.getByName("172.16.23.13")
                , port);

        Settings settings = Settings.builder()
                .put("cluster.name", clusterName)
//                .put("cluster.name", "esfyb_cluster")
                .build();

        TransportClient client = new PreBuiltTransportClient(settings);
//        TransportClient client = TransportClient
//                .builder()
//                .settings(settings)
//                .build();
        client.addTransportAddress(node);

        return client;
    }

}
